#include<stdio.h>
#include<arpa/inet.h>   //for inet_ntoa , struct sockaddr_in
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/socket.h>  
#include<net/if.h>      //for struct ifreq
#include<string.h>      
  int main(void){
        int sockfd_ip;
        struct ifreq ifr;
        struct sockaddr_in sin;
        
        if((sockfd_ip=socket(AF_INET,SOCK_DGRAM,0))<0){
                perror("socket error");
                 return -1;
         }       
         strcpy(ifr.ifr_name,"ens33");
         if(ioctl(sockfd_ip,SIOCGIFADDR,&ifr)<0){ //get eth0 ip
                 perror("ioctl error");
                 return -1;
        }       
         memcpy(&sin,&ifr.ifr_addr,sizeof(sin));
         //sin=(struct sockaddr_in)ifr.ifr_addr;
         
         printf("ip is %s\n",inet_ntoa(sin.sin_addr));
         return 0;
 }       